package com.tian.service.impl;

import com.tian.enums.Sex;
import com.tian.mapper.UserMapperCustom;
import com.tian.mapper.UsersMapper;
import com.tian.pojo.Users;
import com.tian.pojo.bo.UserBO;
import com.tian.service.UserService;
import com.tian.utils.DateUtil;
import com.tian.utils.MD5Utils;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private UserMapperCustom userMapperCustom;

    private static final String USER_FACE="http://122.152.205.72:88/group1/M00/00/05/CpoxxFw_8_qAIlFXAAAcIhVPdSg994.png";

    @Autowired
    private Sid sid;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public boolean queryUsernameIsExist(String username) {
        Example userExample=new Example(Users.class);
        Example.Criteria userExampleCriteria = userExample.createCriteria();
        userExampleCriteria.andEqualTo("username",username);

        Users result = usersMapper.selectOneByExample(userExample);
        return result==null?false:true;
    }





    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Users createUser(UserBO userBO) {

//        try {
//            Thread.sleep(3000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        String userId = sid.nextShort();
        Users user=new Users();
        user.setId(userId);
        user.setUsername(userBO.getUsername());
        try {
            user.setPassword(MD5Utils.getMD5Str(userBO.getPassword()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        //默认用户昵称同用户名
        user.setNickname(userBO.getUsername());
        //默认头像
        user.setFace(USER_FACE);
        //默认生日
        user.setBirthday(DateUtil.stringToDate("1900-01-01"));
        //默认性别为 保密
        user.setSex(Sex.secret.type);

        user.setIs_admin(0);
        user.setMobile("13114785269");
        user.setEmail("123@qq.com");

        user.setCreatedTime(new Date());
        user.setUpdatedTime(new Date());

        usersMapper.insert(user);
        return user;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Users queryUserForLogin(String username, String password) {

//        try {
//            Thread.sleep(2000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

//        Example userExample=new Example(Users.class);
//        Example.Criteria criteria = userExample.createCriteria();
//
//        criteria.andEqualTo("username",username);
//        criteria.andEqualTo("password",password);
//
//        Users result = usersMapper.selectOneByExample(userExample);

        Users result = userMapperCustom.selectUserByUsernameAndPassword(username, password);
        return result;



    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public String queryUserIdByUsername(String username) {
        String userId = userMapperCustom.queryUserIdByUsername(username);
        return userId;
    }


}
